"""全局自定义异常类、返回异常类的方法"""

from typing import Any
from pydantic import ValidationError


# 自定义异常类
class MAIN_EXCEPTION(Exception):
    def __init__(self, message="", code=500) -> None:
        super().__init__()
        self.message = message
        self.code = code


def MAIN_VALUE_ERROR_FUNC(
    loc: tuple = (),
    msg: str = "",
    value: Any = None,
    type: str = "value_error",
    model_name: str = "CustomValidation",
) -> ValidationError:
    """
    Pydantic 2.x 兼容的自定义验证错误生成器
    参数:
        loc: 错误位置元组，如 ("field",) 或 ("nested", "field")
        msg: 错误信息
        value: 引发错误的输入值
        type: 错误类型（默认"value_error"）
    返回:
        ValidationError 实例
    """
    return ValidationError.from_exception_data(
        model_name,
        [
            {
                "type": type,
                "loc": loc,
                "msg": msg,
                "input": value,
                "ctx": {"error": msg},
            }
        ],
    )
